import {
    gameUI
} from "../../ui/layaMaxUI";
import Util from "../util/Util";

export default class game extends gameUI {

    constructor() {
        super();
        //弄个全局遮罩
    }

    onEnable() {
        var parent = this;
        window.currPage = this;
        // window.currPageUtil = Util;

        this["游戏层"].x = Laya.stage.width / 2;
        this["拖动栏"].x = Laya.stage.width - 500;

        this["拖动栏"].on(Laya.Event.MOUSE_DOWN, this, function (e) {

        });
        //添加鼠标抬起事件侦听。抬起时还原按钮。
        this["拖动栏"].on(Laya.Event.MOUSE_UP, this, function (e) {

        });
        this["拖动栏"].on(Laya.Event.MOUSE_MOVE, this, function (e) {
            var bili = (e.stageY - 100) / 550;
            if (bili < 0) {
                bili = 0;
            }
            if (bili > 1) {
                bili = 1;
            }
            parent.roleangle = parseInt((1 - bili) * 150);
            parent.role1._player.playByFrame(2, 1, 1, parent.roleangle);



            // parent.role1.getSlotByName("weaponhood1").currDisplayData.width = parseInt(e.stageY );
            // console.log(bili);
        });

        setTimeout(function () {
            parent.initEvent();
        }, 100);
        Laya.timer.frameLoop(1, this, this.runFun);

        // var guochang = Util.getSk("guochang", function () {
        //     return true;
        // }, 1);
        // guochang.pos(Laya.stage.width / 2, 375);
        // this.addChild(guochang);
        // Util.playSk("Transition", guochang, true);


        // var pipei = Util.getSk("pipei", function () {
        //     return true;
        // }, 1);
        // pipei.pos(Laya.stage.width / 2, 375);
        // this.addChild(pipei);
        // Util.playSk("Match", pipei, true);




        // function bobo(val){currPage.role1.player.playByFrame(1,1,2147483647,val,val+1)}

        var jianxi = 120;
        this.addRole("role1", false, 1500 + jianxi, 750 - 100 - 12 - 44, "发射角度");

        // for(var i=1;i<=11;i+=3){
        //     var chacao=this.role1.getSlotByName("weaponhood"+i);
        // }
        this.role1.getSlotByName("weaponhood1").currDisplayData.width = 30;
        this.role1.getSlotByName("weaponhood1").currDisplayData.height = 300;
        this.role1.getSlotByName("weaponhood1").currDisplayData.transform = new Laya.Transform();
        this.role1.getSlotByName("weaponhood1").replaceSkin(Laya.Loader.getRes('res/game/弓箭.png'));

        this.role1.getSlotByName("weaponhood1").currDisplayData.transform.skX = 90 / (Math.PI / 180);
        // a.setSlotSkin("replace", Laya.Loader.getRes("res/shu/" + uplv + ".png"));
        // this.role1.setSlotSkin('weaponhood6', Laya.Loader.getRes('res/game/弓箭.png'));
        /*
        var oneval = 5000 / 194;
        window.bobo = function (val) {
            // console.log(oneval * val, oneval * (val + 1));
            currPage.role1._player.playByFrame(2, 1, 10, val);
            // currPage.role1.play("发射角度", false, true, oneval * val, oneval * (val + 1));
            // currPage.role1.player.playByFrame(0, 1, 100, val, val + 1);
            // currPage.role1.player.playByFrame(1, 1, oneval * val, val, val + 1);
        }
        var val = 0;
        setInterval(function () {
            // console.log(currPage.role1.player._currentKeyframeIndex);
            bobo(val);
            val++;
            if (val > 150) {
                val = 1;
            }
        }, 1000 / 30);
        // currPage.role1.player.playByFrame(1, 1, 1130, 20, 101)

        //播放一帧, 一共 120 帧
        // .playByFrame(1,1,100,7,17)
        */

        // this.addRole("role2", false, 1500, 750 - 100 - 12 - 44, "庆祝");
        // this.addRole("role3", false, 1500 - jianxi, 750 - 100 - 12 - 44, "晒肌肉");
        // this.addRole("role4", false, 1500 - jianxi*2, 750 - 100 - 12 - 44, "眩晕");
        // this.addRole("role5", false, 1500 - jianxi*3, 750 - 100 - 12 - 44, "被击倒");
        // this.addRole("role6", false, 1500 - jianxi*4, 750 - 100 - 12 - 44, "死亡");
        // this.addRole("role7", false, 1500 - jianxi*5, 750 - 100 - 12 - 44, "吃惊");
        // this.addRole("role8", false, 1500 - jianxi*6, 750 - 100 - 12 - 44, "待发射");


        //  var gongjian = new Laya.Sprite();
        // gongjian.texture = "res/game/弓箭.png";
        // gongjian.pos(-180, -20);
        // this["role1"].addChild(gongjian);


        // var gongjian = new Laya.Sprite();
        // gongjian.texture = "res/game/弓箭.png";
        // gongjian.pos(1500 + jianxi, 750 - 100 - 12 - 44);
        // this["游戏层"].addChild(gongjian);
        // this["弓箭"].pos(1500 + jianxi, 750 - 100 - 12 - 44 - 200);
        // this["弓箭"].zOrder = 20;
        // this["role1"].zOrder = 21;

        var minx = 3000 / Laya.stage.width;
        var miny = 1500 / Laya.stage.height;
        this.minscale = 1 / Math.min(minx, miny);
        this.setScale(this.minscale);

        this.roleangle = 75;
        Laya.Tween.to(this["游戏层"], {
            scaleX: 1,
            scaleY: 1
        }, 1500, Laya.Ease.linearNone, new Laya.Handler(this, function () {
            console.log('完毕');
        }));
    }

    addRole(key, iscale, x, y, name) {
        var parent = this;
        this[key] = Util.getSk("role1", function () {
            if (!parent.isplayok) {
                parent.isplayok = true;
                // parent.role1._player.playByFrame(2, 1, 1, 100);
            }
            // parent[key].paused();
            return true;
        }, false);
        this[key].pos(x, y);
        this[key].scaleX = iscale ? 1 : -1;
        this["游戏层"].addChild(this[key]);
        Util.playSk(name, this[key], true);
        // Util.playSk(name, this[key], true, function () { }, 1, 1);
    }

    setScale(scale) {
        console.log("缩放scale:", scale);
        this["游戏层"].scale(scale, scale);
    }

    initEvent() {
        //初始化点击按钮
        var parent = this;
        parent["返回"].setTapCall(function () {
            console.log("返回");
            parent.goHome();
        });
    }
    goHome() {
        this.destroy(true);
        Laya.stage.addChild(new Util.homeUi());
    }


    runFun() {
        //update
        if (this.role1 && this.isplayok) {
            //roleangle 0-150
            // console.log(this.roleangle);
            // if (this.roleangle != this.roleanglelast) {
            //     this.roleanglelast = this.roleangle;
            //     this.role1._player.playByFrame(2, 1, 1, this.roleangle);
            // }
        }
    }


    onDestroy() {
        Laya.timer.clear(this, this.runFun);
    }
}